﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BL;
using DA;

namespace UI.Controllers
{
    public class SlicesController : Controller
    {
        //
        // GET: /Slices/

        public ActionResult Index()
        {
            SlicesRepository rep = new SlicesRepository();

            return View(rep.GetList());
        }

        //
        // GET: /Slices/Create

        public ActionResult Create()
        {
            return View("Edit");
        } 

        //
        // GET: /Slices/Edit/5
 
        public ActionResult Edit(int? id)
        {
            SlicesRepository rep = new SlicesRepository();

            if (id.HasValue)
            {
                Slice slice = rep.GetSlice(id.Value);

                if (slice != null)
                    return View(rep.GetSlice(id.Value));
                else
                    return RedirectToAction("Error");
            }
            else
            {
                return View();
            }
        }

        //
        // POST: /Slices/Edit/5

        [HttpPost]
        public ActionResult Edit(int? id, Slice slice)
        {
            SlicesRepository rep = new SlicesRepository();

            if (id.HasValue)
            {
                if(rep.UpdateSlice(id.Value, slice))
                    return RedirectToAction("Index");
                else
                    return RedirectToAction("Error");
            }
            else
            {
                if (rep.AddSlice(slice))
                    return RedirectToAction("Index");
                else
                    return RedirectToAction("Error");
            }
        }

        //
        // GET: /Slices/Delete/5
 
        public ActionResult Delete(int id)
        {
            SlicesRepository rep = new SlicesRepository();

            Slice slice = rep.GetSlice(id);

            if (slice != null)
                return View(rep.GetSlice(id));
            else
                return RedirectToAction("Error");
        }

        //
        // POST: /Slices/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, Slice slice)
        {
            SlicesRepository rep = new SlicesRepository();

            if(rep.DeleteSlice(id))
                return RedirectToAction("Index");
            else
                return RedirectToAction("Error");
        }
    }
}
